package com.lin.concurrence;

import java.util.concurrent.*;

public class TestFuture {
	public static void main(String[] args) throws Exception {
		ExecutorService executorService = Executors.newCachedThreadPool();
		Future<Integer> future = executorService.submit(new Task());
		executorService.shutdown();

		TimeUnit.SECONDS.sleep(1);
		System.out.println("主线程执行中...");
		System.out.println("子线程是否计算完成：" + future.isDone());
		System.out.println("计算结果：" + future.get());

	}
}

class Task implements Callable<Integer>{

	@Override
	public Integer call() throws Exception {
		System.out.println("子线程正在计算中...");
		TimeUnit.SECONDS.sleep(3);

		int total = 0;
		for (int i = 0; i < 10000; i++) {
			total += i;
		}
		return total;
	}
}
